<template>
  <a-spin :spinning="confirmLoading" style="background: #fff">
    <a-divider>员工试用期考核表</a-divider>
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息" :forceRender="true">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
                  <select-user-by-dep :disabled="formDisabled" :multi="false" v-model="model.employeeId" disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="24" :hidden="true">
                <a-form-model-item label="单据id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billId">
                  <a-input :disabled="formDisabled" v-model="model.billId" placeholder="请输入单据id" ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="人员编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeNo">
                  <a-input :disabled="formDisabled" v-model="model.employeeNo" placeholder="请输入人员编号" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gender">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.gender"
                    dictCode="gender"
                    placeholder="请选择性别"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionId">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.positionId"
                    dictCode="sys_position,name,id,del_flag = 0 and audit_status=2"
                    placeholder="请选择岗位名称"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="入职时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partTime">
                  <j-date :disabled="formDisabled" placeholder="请选择入职时间" v-model="model.partTime" style="width: 100%" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="试用期满时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
                  <a-date-picker
                    :disabled="formDisabled"
                    :disabled-date="disabledDate"
                    :default-picker-value="this.entryTime"
                    placeholder="请选择试用期满时间"
                    valueFormat="YYYY-MM-DD"
                    v-model="model.endTime"
                    style="width: 100%" />
                  <!-- <a-date-picker :disabled=formDisabled @change="onChange" style="width: 100%" placeholder="请选择试用期满时间" v-model="model.endTime"/> -->
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="评测信息">
            <a-row :gutter="48">
              <div v-for="(item, key) in question" :key="key" >
                <a-col :span="24" style="padding:10px;">
                  <h3 style="font-weight:bold">{{ item.title }}</h3>
                </a-col>
                <a-col :span="24" v-for="(temp, index) in item.children" :key="index" >
                  <a-form-model-item :label="(index+1)+'、'+temp.title" :labelCol="labelCol" :wrapperCol="wrapperCol" :prop="question[key].children[index].code">
                    <j-dict-select-hr v-model="model.question[key].children[index].value" type="checkbox" :data="temp.children" :disabled="formDisabled" @input="handleInput(...arguments,key,index)"></j-dict-select-hr>
                  <!-- <a-checkbox-group :disabled="formDisabled" :value="temp.title"  @change="handleInput" >
                    <a-checkbox v-for="(select, idx) in temp.children" :key="idx" :value="select.code" @change="handleInput" style="width:19%;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;"><j-ellipsis :value="select.title" :length="12"></j-ellipsis></a-checkbox>
                  </a-checkbox-group> -->
                  </a-form-model-item>
                </a-col>
              </div>

            </a-row>
            <!-- <a-row :gutter="48">
              <a-col :span="24" style="padding:10px;">
                <h3 style="font-weight:bold">一、工作业绩与能力</h3>
              </a-col>
              <a-col :span="24" >
                <a-form-model-item label="1、工作效率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="aa">
                  <j-dict-select-tag :disabled="formDisabled" type="checkbox" v-model="model.aa"
                                     dictCode="工作效率" placeholder="请选择工作效率"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="2、专业能力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="aa">
                  <j-dict-select-tag :disabled="formDisabled" type="checkbox" v-model="model.aa"
                                     dictCode="专业能力" placeholder="请选择专业能力"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24" >
                <a-form-model-item label="3、决策能力" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="aa">
                  <j-dict-select-tag :disabled="formDisabled" type="checkbox" v-model="model.aa"
                                     dictCode="决策能力" placeholder="请选择决策能力"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24" style="padding:10px;">
                <h3 style="font-weight:bold">二、职业态度与素养</h3>
              </a-col>
            </a-row> -->
          </a-collapse-panel>
          <a-collapse-panel key="3" header="评测结果">
            <a-row :gutter="48">
              <a-col :span="24" style="background-color:#F1F1FF">
                <div style="font-weight:bold">说明：A优秀 ：95分以上可提前2个月转正；B优良：90分以上可提前1个月转正；C较好：75（含）-90分可正常转正；D一般：60-75分延长试用；E较差：60分以下终止试用。</div>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="考核得分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="evaluatingResult">
                  <a-input :disabled="formDisabled" v-model="model.evaluatingResult" placeholder="请输入考核得分" disabled style="width:65%">
                    <span style="color:#f00;font-weight:bold" slot="addonAfter">{{ result }}</span>
                  </a-input>
                  <a-button type="primary" :disabled="formDisabled" size="small" @click="calculate" style="margin-left:5%">计算得分</a-button>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="评测人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="evaluatingEmployee">
                  <select-user-by-dep :disabled="formDisabled" :multi="false" v-model="model.evaluatingEmployee" disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="评测时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="evaluatingTime">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择评测时间"
                    v-model="model.evaluatingTime"
                    style="width: 100%"
                    disabled
                    :show-time="true"
                    date-format="YYYY-MM-DD HH:mm:ss"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="评价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="evaluationPeriod">
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.evaluationPeriod"
                    rows="4"
                    placeholder="请输入评价"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col>
              <!-- <a-col :span="24">
                <a-form-model-item label="需改进事项" :labelCol="labelCol" :wrapperCol="wrapperCol"
                  prop="needingImprovement">
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.needingImprovement"
                    rows="4"
                    placeholder="请输入需改进事项"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col> -->
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="4" header="附件信息">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                  <j-upload :disabled="formDisabled" v-model="model.attachment"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>

        <a-row :gutter="48" v-if="printShow">
          <a-col :span="24" style="text-align: center;margin-top: 15px">
            <a-button type="primary" v-print="'#regularDetail'">打印</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <ErpHrEmployeeRegularDetailFormPrint
      v-if="printShow"
      id="regularDetail"
      class="print_style"
      :formData="model"
      :question="model.question"
    ></ErpHrEmployeeRegularDetailFormPrint>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { mapGetters } from 'vuex';
import moment from 'moment'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import JEllipsis from '@/components/jeecg/JEllipsis'
import JDictSelectHr from '@/components/dict/JDictSelectHr'
import ErpHrEmployeeRegularDetailFormPrint from './ErpHrEmployeeRegularDetailFormPrint'
import Print from '@/utils/print/printarea.js';
export default {
  name: 'ErpHrEmployeeRegularDetailForm',
  components: {
    SelectUserByDep,
    JEllipsis,
    JDictSelectHr,
    ErpHrEmployeeRegularDetailFormPrint
  },
  props: {
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    printShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      entryTime: '',
      result: '分',
      activeKey: ['1', '2', '3', '4'],
      model: {
        evaluating: {}
      },
      labelCol: {
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        endTime: [
          { required: true, message: '请选择试用期满时间!' }
        ],
        evaluatingResult: [
          { required: true, message: '请点击计算得分按钮!' }
        ]
      },
      url: {
        loadTreeRoot: '/api/erpmng/sys/category/loadTreeRoot',
        add: '/api/erp/hr/regular/erpHrEmployeeRegularDetail/add',
        edit: '/api/erp/hr/regular/erpHrEmployeeRegularDetail/edit',
        queryById: '/api/erp/hr/regular/erpHrEmployeeRegularDetail/queryById'
      },
      question: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
    this.initData();
  },
  methods: {
    disabledDate(current) {
      return current < moment(this.entryTime).subtract('day')
    },
    onChange(date, dateString) {
      this.$set(this.model, 'endTime', dateString);
    },
    handleInput(data, key, index) {
      this.$set(this.model, this.question[key].children[index].code, data);
      let code = this.question[key].children[index].code;
      let evaluating = Object.assign({}, this.model.evaluating);
      evaluating[code] = data;
      this.model.evaluating = evaluating;
      console.log(this.model.evaluating);
      this.$set(this.model, 'evaluatingResult', '');
      this.result = '分'
    },
    // 初始化问题
    initData() {
      let param = {
        async: false,
        pcode: 'A02',
        removeCode:''
      }
      getAction(this.url.loadTreeRoot, param).then(res => {
        if (res.success) {
          let question = res.result;
          this.model.question = question;
          this.question = question;
          for (var z = 0; z < question.length; z++) {
            let item = question[z].children;
            for (var i = 0; i < item.length; i++) {
              let temp = item[i].title;
              this.$set(this.validatorRules, question[z].children[i].code, [{ required: true, message: '请选择' + temp + '!' }]);
            }
          }
        } else {
          this.$message.error(res.message)
        }
      })
    },
    ...mapGetters(['userInfo']),
    calculate() {
      this.validatorRules.evaluatingResult = [];
      this.validatorRules.endTime = []
      this.$refs.form.validate(valid => {
        if (valid) {
          let mark = 0;
          let result = '';
          let evaluating = this.model.evaluating;
          for (var key in evaluating) {
            let str = evaluating[key]
            let level = str.substring(11, 12);
            if (level == '1') {
              mark += 10;
            } else if (level == '2') {
              mark += 9;
            } else if (level == '3') {
              mark += 7;
            } else if (level == '4') {
              mark += 6;
            } else if (level == '5') {
              mark += 5;
            }
          }
          if (mark >= 95) {
            result = 'A优秀'
          } else if (mark >= 90) {
            result = 'B优良'
          } else if (mark >= 75) {
            result = 'C较好'
          } else if (mark >= 60) {
            result = 'D一般'
          } else {
            result = 'E较差'
          }
          this.$set(this.model, 'evaluatingResult', mark);
          this.result = result
        }
      });
    },
    add(data, entryTime) {
      if (data) {
        let userinfo = this.userInfo();
        this.$set(this.modelDefault, 'evaluating', {});
        this.$set(this.modelDefault, 'employeeId', data.employeeId);
        this.$set(this.modelDefault, 'employeeNo', data.employeeNo);
        this.$set(this.modelDefault, 'gender', data.gender);
        this.$set(this.modelDefault, 'positionId', data.positionId);
        this.$set(this.modelDefault, 'partTime', data.entryTime);
        this.$set(this.modelDefault, 'endTime', data.endTime);
        this.$set(this.modelDefault, 'evaluatingEmployee', userinfo.empId)
        this.modelDefault.evaluatingTime = moment().format('YYYY-MM-DD HH:mm:ss');
      }
      this.edit(this.modelDefault, entryTime);
    },
    edit(record, entryTime) {
      this.$refs.form.resetFields();
      Object.assign(this.$data, this.$options.data());
      this.model = Object.assign({}, record);
      this.handleEvaluating(record);
      this.visible = true;
      this.judgeMark();
      this.entryTime = entryTime;
    },
    judgeMark() {
      if (this.model.evaluatingResult) {
        let mark = this.model.evaluatingResult;
        let result = '';
        if (mark >= 95) {
          result = 'A优秀'
        } else if (mark > 90) {
          result = 'B优良'
        } else if (mark >= 75) {
          result = 'C较好'
        } else if (mark > 60) {
          result = 'D一般'
        } else {
          result = 'E较差'
        }
        this.result = result
      }
    },
    // 处理评测数据
    handleEvaluating(record) {
      let param = {
        async: false,
        pcode: 'A02',
        removeCode:''
      }
      getAction(this.url.loadTreeRoot, param).then(res => {
        if (res.success) {
          let question = res.result
          for (var key = 0; key < question.length; key++) {
            let item = question[key].children;
            for (var index = 0; index < item.length; index++) {
              let temp = item[index].title;
              if (record.evaluating) {
                let evaluating = record.evaluating;
                item[index].value = evaluating[item[index].code];
                this.$set(this.model, item[index].code, evaluating[item[index].code]);
              }
              this.$set(this.validatorRules, question[key].children[index].code, [{ required: true, message: '请选择' + temp + '!' }]);
            }
          }
          this.question = question;
          this.$set(this.model, 'question', question);
        } else {
          this.$message.error(res.message)
        }
      })
    },
    submitTempForm() {
      const that = this;
      // 触发表单验证
      this.$emit('ok', this.model);
      this.$emit('complete', false);
    },
    submitForm() {
      const that = this;
      this.validatorRules.evaluatingResult = [
          { required: true, message: '请点击计算得分按钮!' }
        ];
      this.validatorRules.endTime = [
          { required: true, message: '请选择试用期满时间!' }
        ]
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // that.confirmLoading = true;
          this.$emit('ok', this.model);
          this.$emit('complete', true);
        }
      })
    }
  }
}
</script>
